package stream;

import java.util.*;
import java.util.stream.Collectors;

public class StreamDemo3 {
    /*
        筛选（filter）
        筛选，是按照一定的规则校验流中的元素，将符合条件的元素提取到新的流中的操作。
     */
    public static void main(String[] args) {
        //1.创建一个集合
        List<Integer> list = Arrays.asList(1, 2, 4, 30, 6, 40, 11, 0, 9, 10);

        //2.获取集合中最大值，自然排序  max(Integer::compareTo)//升序 取值最大的元素
        Integer max = list.stream().max((a, b) -> a - b).get();
        System.out.println("最大值为："+max);
        //3.获取集合中最小值     min是取值最小的元素
        Integer min = list.stream().min(Integer::compareTo).get();
        System.out.println("最小值为："+min);

        // 4.自定义排序获取最小值
        Integer min1 = list.stream().min(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        }).get();
        System.out.println("最小值为："+min1);

        // 定义字符串集合
        List<String> list1 = Arrays.asList("helloworld", "H5", "java", "python", "php", "java");
        // 5.获取字符串集合中，最长的字符串
        String sMax = list1.stream().max((a, b) -> a.length() - b.length()).get();
        System.out.println("最长的字符串是："+sMax);
        // 6.获取字符串集合中，最短的字符串
        String sMin = list1.stream().min(String::compareTo).get();
        System.out.println("最短的字符串是："+sMin);

        //7. 统计集合中元素的个数
        long count = list1.stream().count();
        System.out.println("元素个数："+count);

        // 8.集合中java出现的次数
        long count1 = list1.stream().filter(s -> s.contains("java")).count();
        System.out.println("java出现的次数是："+count1);

        // 9.定义一个新对象集合
        ArrayList<Person> list2 = new ArrayList<>();
        list2.add(new Person("小六a", 5000, "深圳"));
        list2.add(new Person("张三", 20000, "北京"));
        list2.add(new Person("李四", 10000, "上海"));
        list2.add(new Person("王五", 8000, "广州"));
        // 获取薪资最高的元素
        Person p1 = list2.stream().max((a, b) -> (int) (a.getSalary() - b.getSalary())).get();
        System.out.println("薪资最高的元素是："+p1);
        // 获取薪资最低的
        Person p3 = list2.stream().min((a, b) -> (int) (a.getSalary() - b.getSalary())).get();
        System.out.println("薪资最低的元素是："+p3);

        List<Person> collect = list2.stream().filter(p -> p.getName().contains("六") && p.getAddress().equals("深圳")).collect(Collectors.toList());

        // 10.获取名字最长的元素
        Person p2 = list2.stream().max((a, b) -> a.getName().length() - b.getName().length()).get();
        System.out.println("名字最长的元素是："+p2);
    }
}
class Person{
    private String name;
    private double salary;
    private String address;
    public Person(String name, double salary, String address) {
        this.name = name;
        this.salary = salary;
        this.address = address;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Double.compare(person.salary, salary) == 0 && Objects.equals(name, person.name) && Objects.equals(address, person.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, salary, address);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", address='" + address + '\'' +
                '}';
    }
}
